<template>
  <div class="login-container">
    <div class="login-card">
      <h2>登录</h2>
      <form @submit.prevent="handleLogin">
        <!-- 错误消息显示 -->
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        <div class="input-group">
          <label for="username">用户名</label>
          <input v-model="credentials.username" type="text" id="username" required />
        </div>
        <div class="input-group">
          <label for="password">密码</label>
          <input v-model="credentials.password" type="password" id="password" required />
        </div>
        <button type="submit" :disabled="isLoading">
          {{ isLoading ? '正在登录....' : '登录' }}
        </button>
      </form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';
import { login as  loginAPI } from '../../api/login.ts';
import router from "../../router"; // 导入登录服务
import {useUserStore} from "../../store/user.ts";
import {initBackEndControlRoutes} from "../../router/initRouter.ts";

export default defineComponent({
  name: 'Login',
  setup() {
    const credentials = ref({
      username: '',
      password: ''
    });

    const isLoading = ref(false);
    const errorMessage = ref('');

    const handleLogin = async () => {
      isLoading.value = true;
      errorMessage.value = '';

      try {
        const response = await loginAPI(credentials.value.username, credentials.value.password);

        if (response.success) {
          const { access_token, refreshToken, username } = response.data!;
          // 登录成功，进行页面跳转或其他操作
          console.log('登录成功');
          // 假设我们跳转到主页
          let userStore = useUserStore();
          userStore.login(access_token, refreshToken, { username });
          await initBackEndControlRoutes();
          console.log(222222);
          await router.push({path: '/'});
        } else {
          // 显示错误信息
          errorMessage.value = response.message || '登录失败，请重试';
        }
      } catch (error) {
        // 处理网络错误或其他异常
        errorMessage.value = '发生错误，请检查您的网络连接';
        console.error('登录出错:', error);
      } finally {
        isLoading.value = false;
      }
    };

    return {
      credentials,
      errorMessage,
      isLoading,
      handleLogin
    };
  }
});
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-card {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 300px;
}

h2 {
  text-align: center;
  margin-bottom: 1.5rem;
}

.input-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  font-weight: bold;
  margin-bottom: 0.5rem;
}

input {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #0056b3;
}

.error-message {
  color: red;
  margin-bottom: 1rem;
}
</style>
